
#include <BFont>
#include <BImage>
#include <BPainter>
#include <BMouseEvent>

#include <BActionGroup>
#include "member_BActionGroup.h"
#include "member_BAction.h"

using namespace BWE;

#define member					(*(member_BActionGroup*)_ptr)
#define member_allocate()		_ptr = new member_BActionGroup(this)
#define member_release()		delete (member_BActionGroup*)_ptr

BActionGroup::BActionGroup(const BString& name) : BObject(name)
{
	member_allocate();
}
BActionGroup::~BActionGroup()
{
	member_release();
}

void BActionGroup::setSelectMode(SelectMode selectMode)
{
	if (member.selectMode != selectMode)
	{
		member.selectMode = selectMode;
		if (selectMode != Select_Multiple)
		{
			for (int i = 0; i < member.actions.size(); i++)
			{
				BAction* action = member.actions[i];
				if (action->checked())
				{
					while (++i < member.actions.size())
					{
						member.actions[i]->setChecked(false);
					}
					break;
				}
			}
		}
	}
}
SelectMode BActionGroup::selectMode() const
{
	return member.selectMode;
}

bool BActionGroup::addAction(BAction* action)
{
	if (action)
	{
		action->setCheckable(true);
		member.actions.append(action);
		member.connect(action, Signal_Checked, &member_BActionGroup::slotActionChecked);
		member.connect(action, Signal_Triggered, &member_BActionGroup::slotActionTriggler);
		return true;
	}
	return false;
}
BAction* BActionGroup::addAction(const BString& text)
{
	BAction* action = new BAction(text);
	addAction(action);
	return action;
}
BAction* BActionGroup::addAction(BIcon* icon, const BString& text)
{
	BAction* action = new BAction(icon, text);
	addAction(action);
	return action;
}
bool BActionGroup::removeAction(BAction* action)
{
	if (!action)
		return false;
	if (member.actions.contain(action))
	{
		member.disconnect(action);
		member.actions.remove(action);
	}
	return true;
}

BAction* BActionGroup::checkedAction() const
{
	return member.checkedAction;
}

void BActionGroup::uncheck()
{
	if (member.checkedAction)
	{
		member.checkedAction->setChecked(false);
	}
}

int BActionGroup::actionCount() const
{
	return member.actions.size();
}
BAction* BActionGroup::action(int index)
{
	return member.actions(index);
}
const BAction* BActionGroup::action(int index) const
{
	return member.actions(index);
}

BAction* BActionGroup::action(const BString& text)
{
	if (text.empty())
		return 0;
	for (int i = 0; i < member.actions.size(); i++)
	{
		BAction* action = member.actions[i];
		if (action->text() == text)
			return action;
	}
	return 0;
}
const BAction* BActionGroup::action(const BString& text) const
{
	if (text.empty())
		return 0;
	for (int i = 0; i < member.actions.size(); i++)
	{
		const BAction* action = member.actions[i];
		if (action->text() == text)
			return action;
	}
	return 0;
}

BAction* BActionGroup::actionOfAsset(const BValue& asset)
{
	if (asset.empty())
		return 0;
	for (int i = 0; i < member.actions.size(); i++)
	{
		BAction* action = member.actions[i];
		if (action->asset() == asset)
			return action;
	}
	return 0;
}
const BAction* BActionGroup::actionOfAsset(const BValue& asset) const
{
	if (asset.empty())
		return 0;
	for (int i = 0; i < member.actions.size(); i++)
	{
		const BAction* action = member.actions[i];
		if (action->asset() == asset)
			return action;
	}
	return 0;
}
